Channel Class
The Channel class implements an push channel and
- Constructs a CometD channel path based on the provided scope.
- Automatically reuses existing subscriptions for the same path if available.
- Provides methods to publish data to the channel and to subscribe/unsubscribe from it.
- Supports automatic cleanup of duplicate subscriptions.
Class signature
export default class Channel {
constructor(scope: ChannelScope)
publish(content: FIXME): Promise<Message | Message[]>
subscribe(update: (data: unknown) => unknown, options?: { inert?: boolean }): Promise<SubscriptionHandle>
unsubscribe(): Promise<void>
unsubscribeAll(): Promise<void>
}
Properties
path(string): The computed CometD channel path based on the scope.update(Callback | undefined): A callback function invoked when a message is received.subscription(SubscriptionHandle | null): The current active CometD subscription handle, if any.
Constructor
Constructs the channel path using the provided scope. If a subscription already exists for this path, it is reused.
Definition
constructor(scope: ChannelScope)
scope(ChannelScopeobject): An object defining the channel’s namespace, including:scopeBoundary: Entity typescopeKey: Instance key (GUID)pushCategory: Define the type of the push channel. Takes a value of thePUSH_CATEGORYenum.
learn more
To learn about scope properties, read Defining scope.
Methods
Publish
Publishes a message to the channel. Returns a promise resolving to one or more published messages.
Definition
publish(content: FIXME): Promise<Message | Message[]>
Parameters
content(FIXME): The content to be published.
Subscribe
Returns the SubscriptionHandle from CometD. Subscribes to the channel and sets the update handler.
Definition
subscribe(update: (data: unknown) => unknown, options?: { inert?: boolean }): Promise<SubscriptionHandle>
Parameters
update: Callback to run when a new message is received.options({ inert?: boolean }, optional): Additional options (e.g.inertto suppress handlers).
Example
import { Channel, authAdapter, SCOPE_BOUNDARY, PUSH_CATEGORY } from 'epicenter-libs';
const session = authAdapter.getLocalSession();
const channel = new Channel({
scopeBoundary: SCOPE_BOUNDARY.GROUP,
scopeKey: session.groupKey,
pushCategory: PUSH_CATEGORY.CHAT,
});
channel.subscribe((data) => {
console.log(data.content);
});
Unsubscribe
Removes the current subscription from CometD and clears the subscription handle.
Definition
unsubscribe(): Promise<void>
Unsubscribe all
Removes all existing subscriptions from the CometD adapter.
Definition
unsubscribeAll(): Promise<void>